// 车辆品牌处理函数模块

const db = require('../mysql/index')
const path = require('path')
const fs = require('fs')

// 获取车辆品牌列表
exports.getBrandList = (req, res) => {
    let { pageNumber, pageSize, brand } = req.body
    let sql = ''
    // 分页
    pageSize = Number(pageSize)
    const offset = (Number(pageNumber) - 1) * pageSize
    // 判断有无品牌参数
    brand ? sql = `SELECT * FROM brand where brand_name_ch like "%${brand}%"` : sql = 'SELECT * FROM brand'
    db.query(sql, (err, result) => {
        if (err) return res.cc(err)
        if (result.length === 0) return res.send({ code: 201, message: '车辆品牌列表为空' })
        // 图片路径处理
        result.forEach(item => {
            item.brand_logo = 'http://62.234.193.89:3007/' + item.brand_logo
        });
        let data = result.slice(offset, offset + pageSize)
        return res.send({
            code: 200,
            message: '获取品牌列表成功',
            data,
            total: result.length
        })
    })
}

// 新增车辆品牌
exports.brandAdd = (req, res) => {
    let { brand_name_ch, brand_name_en, brand_logo, status = 1 } = req.body
    // 先查询品牌是否存在
    const selectSql = 'SELECT * FROM brand WHERE brand_name_ch = ?'
    db.query(selectSql, brand_name_ch, (selectErr, selectResult) => {
        if (selectErr) return res.cc(selectErr)
        if (selectResult.length !== 0) return res.cc('该品牌已存在')
        // 添加品牌
        const sql = 'INSERT INTO brand(brand_name_ch, brand_name_en, brand_logo, status) VALUES(?, ?, ? , ?)'
        db.query(sql, [brand_name_ch, brand_name_en, brand_logo, status], (err, result) => {
            if (err) return res.cc(err)
            if (result.affectedRows !== 1) return res.cc('品牌添加失败')
            return res.send({
                code: 200,
                message: '添加车辆品牌成功'
            })
        })
    })
}

// 获取车辆品牌详情
exports.brandDetail = (req, res) => {
    const id = req.query.id
    const sql = 'SELECT * FROM brand WHERE id = ?'
    db.query(sql, id, (err, result) => {
        if (err) return res.cc(err)
        if (result.length !== 1) return res.cc('获取车辆品牌详情失败')
        // result[0].brand_logo = path.join('http://localhost:3007/', result[0].brand_logo)
        result[0].brand_logo = 'http://62.234.193.89:3007/' + result[0].brand_logo
        return res.send({
            code: 200,
            message: '获取车辆品牌详情成功',
            data: result[0]
        })
    })
}

// 修改车辆品牌
exports.brandEdit = (req, res) => {
    let { brand_name_ch, brand_name_en, brand_logo, status = 1, id } = req.body
    const brand_logo_arr = brand_logo.split('/')
    brand_logo = brand_logo_arr[brand_logo_arr.length - 1]
    const sql = 'UPDATE brand set brand_name_ch = ?, brand_name_en = ?, brand_logo = ?, status = ? WHERE id = ?'
    db.query(sql, [brand_name_ch, brand_name_en, brand_logo, status, id], (err, result) => {
        if (err) return res.cc(err)
        if (result.affectedRows !== 1) return res.cc('修改车辆品牌失败')
        return res.send({
            code: 200,
            message: '修改车辆品牌成功'
        })
    })
}

// 删除车辆品牌
exports.brandDelete = (req, res) => {
    const id = req.query.id
    // 删除logo文件
    const selectSql = 'SELECT brand_logo from brand WHERE id = ?'
    db.query(selectSql, id, (err, result) => {
        if (err) return res.cc(err)
        if (result.length === 1) {
            // 删除文件
            fs.unlink(path.join('./uploads', result[0].brand_logo), (err2) => {
                if (err2) console.log(err2);
            })
        }
    })
    // 删除品牌
    const sql = 'DELETE FROM brand WHERE id = ?'
    db.query(sql, id, (err, result) => {
        if (err) return res.cc(err)
        if (result.affectedRows !== 1) return res.cc('删除车辆品牌失败')
        return res.send({
            code: 200,
            message: '删除车辆品牌成功'
        })
    })
}

// 修改品牌禁启用状态
exports.brandChangeStatus = (req, res) => {
    let { id, status } = req.query
    const sql = 'UPDATE brand set status = ? WHERE id = ?'
    db.query(sql, [status, id], (err, result) => {
        if (err) return res.cc(err)
        if (result.affectedRows !== 1) return res.cc('修改状态失败')
        return res.send({
            code: 200,
            message: '修改车辆品牌状态成功'
        })
    })
}